//
//  SNOrderFormDetailDataManager.m
//  app
//
//  Created by 王少宁 on 15/5/6.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "SNOrderFormDetailDataManager.h"

@interface SNOrderFormDetailDataManager ()
@property (nonatomic, retain) SNUniteUrl * uniteUrl;
@property (nonatomic, retain) JKHttpClient * http;
@end

@implementation SNOrderFormDetailDataManager

+(SNOrderFormDetailDataManager *)shareManager
{
    AS_SINGLETON(SNOrderFormDetailDataManager, manager, {
        manager = [[SNOrderFormDetailDataManager alloc] init];
        manager.uniteUrl = [SNUniteUrl shareUniteUrl];
        manager.http = [[JKHttpClient alloc] init];
    })
    return manager;
}

#pragma mark - 订单详情的请求
- (void)loadOrderFormDetailData:(SNOrderFormDetailHttpResult *)result sn:(NSString *)sn
{
    if ([self.http isRunning]) {
        return;
    }
    
    result.failure = YES;
    result.success = NO;
    
    NSDictionary * params = @{@"sn":sn};
    
    DLog(@"%@ %@",self.uniteUrl.sbpd_orderFormDetailUrl,sn);
    
    [self.http get:self.uniteUrl.sbpd_orderFormDetailUrl params:params result:^(JKResponse *resp) {

        DLog(@"%@",resp.content);
        
        [resp refreshTheDifferentOfNativeTimeAndWebTime];
        
        if (resp.OK) {
            result.success = YES;
            result.failure = NO;
            if (resp.status == JKResponseOK){
                
                result.model = [SNOrderFormDetailModel modelWithDict:resp.content];
            } else if (resp.status == JKResponseTail) {

            }
        } else if (resp.failure) {
            result.failure = YES;
            result.success = NO;
        }
        [result invoke];
    }];
}

#pragma mark - 请求支付宝的私钥,待签名字符串
-(void)requestZFBSignWithResult:(SNZFBSignHttpResult *)result sn:(NSString *)sn
{
    if ([self.http isRunning]) {
        return;
    }
    
    [self.http showNetUnReachableToast];
    
    result.success = NO;
    
    NSDictionary * parmas = @{@"sn":sn};
    
    [self.http get:self.uniteUrl.sbpd_ZFBSignUrl params:parmas result:^(JKResponse *resp) {
        if (resp.OK) {
            
            DLog(@"%@",resp.content);
        
            if (resp.status == JKResponseOK) {
                
                result.success = YES;
                result.failure = NO;
                
                result.sign = resp.content[@"sign"];
                result.newsign = [self encodeString:resp.content[@"newsign"]];
                
            } else if (resp.status == JKResponseTail) {
                
            }
        } else if (resp.failure) {
            result.failure = YES;
            result.success = NO;
        }
        [result invoke];
    }];
}

//URLEncode
- (NSString*)encodeString:(NSString*)unencodedString{
    
    // CharactersToBeEscaped = @":/?&=;+!@#$()~',*";
    // CharactersToLeaveUnescaped = @"[].";
    
    NSString *encodedString = (NSString *)
    CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                              (CFStringRef)unencodedString,
                                                              NULL,
                                                              (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                              kCFStringEncodingUTF8));
    
    return encodedString;
}

#pragma mark - 取消订单的请求
- (void)requestCancleOrderWithResult:(SNCancleOrderHttpResult *)result sn:(NSString *)sn
{
    if ([self.http isRunning]) {
        return;
    }
    
    result.failure = YES;
    result.success = NO;
    
    NSDictionary * params = @{@"sn":sn};
    
    [self.http post:self.uniteUrl.sbpd_cancleOrderFormUrl params:params result:^(JKResponse *resp) {
        if (resp.OK) {
            result.success = YES;
            result.failure = NO;
        } else if (resp.failure) {
            result.failure = YES;
            result.success = NO;
        }
        [result invoke];
    }];
}

#pragma mark - 取消订单之后把订单中的商品添加到购物车
- (void)requestCancleAndAddGoodsToCarWithResult:(SNAddGoodsToCarOrderHttpResult *)result goodsArray:(NSString *)goodsArray
{
    if ([self.http isRunning]) {
        return;
    }
    
    result.failure = YES;
    result.success = NO;
    
    [self.http post:self.uniteUrl.sbpd_addGoodsFromOrderToMyCarUrl params:@{@"goods":[HZBase64Handler encodeBase64WithString:goodsArray]} result:^(JKResponse *resp) {
        if (resp.OK) {
            result.success = YES;
            result.failure = NO;
        } else if (resp.failure) {
            result.failure = YES;
            result.success = NO;
        }
        [result invoke];
    }];
}

@end





#pragma mark - 微信支付
@interface SNOrderWXSignDataManager ()
@property (nonatomic, retain) SNUniteUrl * uniteUrl;
@property (nonatomic, retain) JKHttpClient * http;
@end

@implementation SNOrderWXSignDataManager

+ (SNOrderWXSignDataManager *)shareManager
{
    AS_SINGLETON(SNOrderWXSignDataManager, manager, {
        manager = [[SNOrderWXSignDataManager alloc] init];
        manager.uniteUrl = [SNUniteUrl shareUniteUrl];
        manager.http = [[JKHttpClient alloc] init];
    })
    return manager;
}

- (void)requestWXSignWithResult:(SNWXSignHttpResult *)result sn:(NSString *)sn
{
    if ([self.http isRunning]) {
        return;
    }
    
    result.failure = YES;
    result.success = NO;
    
    [self.http get:self.uniteUrl.sbpd_WXSignUrl params:@{@"sn":sn} result:^(JKResponse *resp) {
        if (resp.OK) {
            
            DLog(@"%@",resp.content);
            
            result.success = YES;
            result.failure = NO;
            if (resp.status == JKResponseOK) {
                
                result.appId = resp.content[@"appId"];
                result.partnerId = resp.content[@"partnerId"];
                result.prepayId = resp.content[@"prepayid"];
                result.nonceStr = resp.content[@"noncestr"];
                result.packageValue = @"Sign=WXPay";
                result.newsign = resp.content[@"newSign"];
                result.sign = resp.content[@"sign"];
                result.timeStamp = resp.content[@"timeStamp"];
                result.sn = resp.content[@"sn"];
                result.orderTitle = resp.content[@"orderTitle"];
                
            } else if (resp.status == JKResponseTail) {
                
            }
        } else if (resp.failure) {
            result.failure = YES;
            result.success = NO;
        }
        [result invoke];
    }];
}

@end



